<?php
/**
 * Created by PhpStorm.
 * User: Duke
 * Date: 2019/11/25
 * Time: 16:15
 */
namespace App\Http\Logic\admin;

use App\Model\AdminMenuModel;
use App\Model\AdminUserModel;
use App\Model\ArticleModel;
use App\Model\CommentModel;
use Illuminate\Http\Request;

class ArticleLogic
{
    //获取左侧分类全部列表
    public function CategoryLeftList()
    {
        $user_model = new AdminMenuModel();
        $i = $user_model->getList(true);
        foreach ($i as $key => $value) {
            $i[$key]['son'] = $user_model->getList(true, $value['id']);
        }
        return $i;
    }
    //获取全部列表
    public function articleAllList(){
        $user_model = new ArticleModel();
        return  $user_model->getConditionList(['state' => 1]);
    }
    /**
     * 递归实现无限极分类
     * @param $array 分类数据
     * @param $pid 父ID
     * @param $level 分类级别
     * @return $list 分好类的数组 直接遍历即可 $level可以用来遍历缩进
     */
    protected function getTree($array, $pid = 0, $level = 0)
    {
        //声明静态数组,避免递归调用时,多次声明导致数组覆盖
        static $list = [];
        foreach ($array as $key => $value) {
            //第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
            if ($value['pid'] == $pid) {
                //父节点为根节点的节点,级别为0，也就是第一级
                $value['level'] = $level;
                //把数组放到list中
                $list[] = $value;
                //把这个节点从数组中移除,减少后续递归消耗
                unset($array[$key]);
                //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
                $this->getTree($array, $value['id'], $level + 1);
            }
        }

        return $list;
    }

    //获取菜单指定列表
    public function getCategoryList($pid){
        $user_model = new AdminMenuModel();
        return $user_model->getList(true, $pid);
    }
    // 添加文章
    public function insertArticle($data){
        $user_model = new ArticleModel();
        $user = new AdminUserModel();
        $uid = $user->findInfo(['username' => session('username')]);
        $post = [
            'uid'     =>  $uid['id'],
            'title'   =>  $data['title'],
            'cid'     =>  $data['category'],
            'content' =>  $data['desc'],
            'addtime' =>  time(),
        ];
        return $user_model->insertInfo($post);

    }
    // 更改文章
    public function updateArticle($post){
        $user_model = new ArticleModel();
        $user = new AdminUserModel();
        $uid = $user->findInfo(['username' => session('username')]);
        $data = [
            'cid'     =>  $post['category'],
            'uid'     =>  $uid['id'],
            'title'   =>  $post['title'],
            'content' =>  $post['desc'],
            'addtime' =>  time(),
        ];
        return $user_model->update_info($post['aid'],$data);

    }
    // 删除文章
    public function delArticle($id){
        $user_model = new ArticleModel();
        $user_model->del($id);
        return true;
    }
    // 获取文章信息
    public function findArticleInfo($id){
        $user_model = new ArticleModel();
        $array = ['id'=>$id];
        return $user_model->find_info($array);
    }
    // 获取回收站文章列表
    public function getConditionList($name){
        $article_model = new ArticleModel();
        $user = new AdminUserModel();
        $userData = ['username' => $name];
        $uid = $user->findInfo($userData);
        $array = [
            'uid'=>$uid['id'],
            'state'=> "0"
        ];
        return $article_model->getConditionList($array);
    }
    // 评论列表
    public function commentListLogic($aid){
        $comment_model = new CommentModel();
        return $comment_model->getConditionList(['aid'=>$aid]);
    }
}
